import request from "./request"
import {
  cache
} from "./cache"
import store from '@/store'
import {
  getKey,
  getDynamicKey
}
from "./key"
import nextFunction from "./queue"
var md;
var pullKey = function (data, next) {
  getKey().then(res => {
    next()
  })
}
var pullDyKey = function (data, next) {
  getDynamicKey().then(res => {
    if (store.getters.dynamicCode) {
      md.params.uid = store.getters.dynamicCode.user.uid
    }
    next()
  })
}
var datas = function (data, next) {
  return new Promise(function (resolve) {
    request(md).then((res) => {
      resolve(res)
      next()
    }).catch(err => {})
  })
}
const $http = {
  interceptorsBefor(data) {
    md = data;
    md.params.bid = "yw";
    md.params.uid = "";
    if(store.getters.dynamicCode){
      md.params.uid=store.getters.dynamicCode.user.uid
    }
    return new Promise(function (resolve) {
      if (!store.getters.dynamicCode && data.isDynamic) {
        nextFunction.nextRegister(pullKey, pullDyKey, datas).then(res => {
          resolve(res)
        })
      } else {
        if (!store.getters.key) {
          nextFunction.nextRegister(pullKey, datas).then(res => {
            resolve(res)
          })
        } else {
          datas().then(res => {
            resolve(res)
          })
        }
      }
    })


  }
}
export default $http
